/**
 * 通知记录管理初始化
 */
var NotifyRecord = {
    id: "NotifyRecordTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
NotifyRecord.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        	{title: '商户编号', field: 'merchantNo', visible: true, align: 'center', valign: 'middle'},
        	{title: '商户订单号', field: 'outOrderNo', visible: true, align: 'center', valign: 'middle'},
        	{title: '平台订单号', field: 'platTradeNo', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '最后通知时间', field: 'lastNotifyTime', visible: true, align: 'center', valign: 'middle'},
            {title: '通知次数', field: 'notifyTimes', visible: true, align: 'center', valign: 'middle'},
            {title: '限制通知次数', field: 'limitNotifyTimes', visible: true, align: 'center', valign: 'middle'},
            {title: '通知失败次数', field: 'notifyFailTimes', visible: true, align: 'center', valign: 'middle'},
            {title: '通知状态', field: 'status', visible: true, align: 'center', valign: 'middle',formatter:"statusFormatter"},
            {title: '通知类型', field: 'notifyType', visible: true, align: 'center', valign: 'middle',formatter:"notifyTypeFormatter"}
    ];
};


function statusFormatter(value, row, index){
	if (value == 100) {
		return '<span class="label label-success">成功</span>';
	} else if(value == 101){
		return '<span class="label label-danger">未成功</span>';
	}
}

function notifyTypeFormatter(value, row, index){
	if (value == 100) {
		return '<span class="label label-success">自动</span>';
	} else if(value == 101){
		return '<span class="label label-danger">手动</span>';
	}
}

/**
 * 检查是否选中
 */
NotifyRecord.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        NotifyRecord.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加通知记录
 */
NotifyRecord.openAddNotifyRecord = function () {
    var index = layer.open({
        type: 2,
        title: '添加通知记录',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/notifyRecord/notifyRecord_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看通知记录详情
 */
NotifyRecord.openNotifyRecordDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '通知记录详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/notifyRecord/notifyRecord_update/' + NotifyRecord.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除通知记录
 */
NotifyRecord.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/notifyRecord/delete", function (data) {
            Feng.success("删除成功!");
            NotifyRecord.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("notifyRecordId",this.seItem.id);
        ajax.start();
    }
};

NotifyRecord.formParams = function() {
    var queryData = {};
    queryData['merchantNo'] = $("#merchantNo").val();
    queryData['outOrderNo'] = $("#outOrderNo").val();
    queryData['platTradeNo'] = $("#platTradeNo").val();

    return queryData;
}

/**
 * 查询交易流水列表
 */
NotifyRecord.search = function () {
	NotifyRecord.table.refresh({query: NotifyRecord.formParams()});
};



$(function () {
    var defaultColunms = NotifyRecord.initColumn();
    var table = new BSTable(NotifyRecord.id, "/notifyRecord/list", defaultColunms);
    table.setPaginationType("server");//服务器端分页
    table.setQueryParams(NotifyRecord.formParams());
    NotifyRecord.table = table.init();
});
